package cn.nuc.controller.system.back;

import cn.nuc.pojo.system.User;
import cn.nuc.pojo.entity.ResponseData;
import cn.nuc.service.system.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/system/user")
@Api(tags = "关于用户的接口")
public class UserController {
    @Autowired
    private IUserService userService;
    @GetMapping("/userList")
    @ApiOperation(value = "获取所有用户信息")
    public ResponseData<User> findAllUser(String username){
        System.out.println(username);
        List<User> userList = null;
        if(username == null || StringUtils.isEmpty(username)){
            userList = userService.list();
        }else{
            userList = userService.list(new QueryWrapper<User>().like("username" , username));
        }
        if(userList != null){
            return ResponseData.success().putDataVule("userList" , userList);
        }
        return ResponseData.serverInternalError();
    }
    @PostMapping("/addOrUpdateUser")
    @ApiOperation(value = "添加或要修改信息的用户")
    @ApiImplicitParam(value = "要添加或修改的用户对象"  ,name = "user" , required = true , paramType = "body" , dataType = "User")
    public ResponseData<User> addUser(@RequestBody User user){
        if(user.getId() == null){
            List<User> username = userService.list(new QueryWrapper<User>().eq("username", user.getUsername()));
            if(username.size() != 0){
                return ResponseData.cusstomerError("此用户名已存在");
            }
        }
        String hashpw = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
        user.setPassword(hashpw);
        boolean res = userService.saveOrUpdate(user);
        if(res){
            return ResponseData.success().putDataVule("saveRes" , res);
        }
        return ResponseData.serverInternalError();
    }
    @DeleteMapping("/deleteUser")
    @ApiOperation(value = "删除用户"  , notes = "根据id删除用户")
    @ApiImplicitParam(value = "要删除的用户id", name = "id" , required = true , dataType = "Integer" , paramType = "query")
    public ResponseData<User> deleteUser(Integer id){
        boolean remove = userService.removeById(id);
        if(remove){
            return ResponseData.success().putDataVule("saveRes" , remove);
        }
        return ResponseData.serverInternalError();
    }
    @GetMapping("/getOne")
    public ResponseData<User> getOneById(Integer id){
        List<User> userList = userService.list(new QueryWrapper<User>().eq("id", id));
        if(userList != null){
            return ResponseData.success().putDataVule("user" , userList.get(0));
        }else{
            return  ResponseData.serverInternalError();
        }
    }

}
